#/bin/bash
#System 
if [ -e /etc/redhat-release ]; then
#sys centos
 OS=CentOS
 [ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=7
 [ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=6
 echo -e "\033[32m $OS $OS_Ver \033[0m" && sleep 3
 yum install epel-release vnc-server tigervnc-server tigervnc-server-module -y
 yum groupinstall "X Window system" "xfce" -y
 cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:1.service
cat >/etc/systemd/system/vncserver@:1.service<<EOF
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=forking

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target
EOF

 echo "" && echo -e "\033[32m Vncpasswd: \033[0m" && vncpasswd
 systemctl daemon-reload && systemctl enable vncserver@:1.service && systemctl start vncserver@:1.service
echo '#!/bin/sh
# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
#exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
startxfce4 & 
'>/root/.vnc/xstartup
systemctl restart vncserver@:1.service && echo "" && echo -e "\033[32m Done \033[0m"
#systemctl isolate graphical.target && systemctl set-default graphical.target
#sys Debian
  elif [ -n "$(grep -i 'Debian' /etc/issue 2> /dev/null)" ]; then
    OS=Debian
	echo -e "\033[32m $OS \033[0m" && sleep 3
	apt-get update && apt-get install xorg xdm xfce4
  elif [ -n "$(grep -i 'Ubuntu' /etc/issue 2> /dev/null)" ]; then
    OS=Ubuntu
	echo -e "\033[32m $OS \033[0m" && sleep 3
	apt-get update && apt-get install xorg xdm xfce4
  else
    echo "UnknownOS"
	exit
fi

echo -e "\033[32m Done. \033[0m"